package cn.hotel.servlet;

import cn.hotel.pojo.Log;
import cn.hotel.service.LogService;
import cn.hotel.service.LogServiceImpl;
import cn.hotel.utlis.Paging;
import cn.hotel.utlis.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/log")
public class LogServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();

        LogService logService = new LogServiceImpl();
        String type = request.getParameter("type");
        type = type == null? "inquiryLog" : type;
        switch (type){
            case "inquiryLog":
                //查询
                inquiry(request,response,logService);
                break;
            case "addLog":
                //添加
                add(request,request,logService,out);
                break;
            case "deleteLog":
                //删除
                logDelete(request,response,logService,out);
                break;
        }
    }

    //删除日志
    private void logDelete(HttpServletRequest request, HttpServletResponse response, LogService logService, PrintWriter out) throws ServletException, IOException {
        String deleteLog = request.getParameter("name");
        String appPath = request.getContextPath();

        Map<String ,Object> deleteMap = logService.deleteLog(deleteLog);
        if("1".equals(deleteMap.get("flag"))){
            out.print("<script>alert('删除成功');location.href='"+appPath+"/log?type=inquiryLog'</script>");
            //request.getRequestDispatcher("logjsp/log.jsp").forward(request,response);
            out.flush();
        }else {
            out.print("<script>alert('删除失败');location.href='"+appPath+"/log?type=inquiryLog'</script>");
            //request.getRequestDispatcher("logjsp/log.jsp").forward(request,response);
            out.flush();
        }
    }

    //添加日志
    private void add(HttpServletRequest request, HttpServletRequest request1, LogService logService, PrintWriter out) {
        //获取操作人
        String operator = request.getParameter("");
        //获取操作信息
        String content =request.getParameter("");
        String appPath = request.getContextPath();

        Map<String ,Object> addMap = logService.addLog(operator,content);
        if("1".equals(addMap.get("flag"))){
            out.print("<script>alert('添加成功');location.href='"+appPath+"/log?type=inquiryLog'</script>");

        }else {
            out.print("<script>alert('添加失败')location.href='"+appPath+"/log?type=inquiryLog'</script>");
        }
        out.flush();
    }

    //分页查询日志
    private void inquiry(HttpServletRequest request, HttpServletResponse response, LogService logService) throws ServletException, IOException {
        Paging<Log> logPaging = new Paging<>();

        //获取名字
        String operators = request.getParameter("operators");
        //获取当前页
        String num = request.getParameter("current");
        int nums = StringUtil.strToIntFirst(num);

        //获取总条数
        if("".equals(operators)){
            operators = null;
        }
        Map<String ,Object> objectMap = logService.elaboration(operators);
        if("1".equals(objectMap.get("flag"))){
            int sum = (int) objectMap.get("data");
            logPaging.setSumData(sum);
        }
        //边界处理
        nums = nums > logPaging.getSumPage()? logPaging.getSumPage() : nums;
        //发送当前页
        logPaging.setCurrent(nums);
        //获取数据
        Map<String,Object> inquiryMap = logService.logMap(operators, logPaging.getCurrent(), logPaging.getNumData());
        if("1".equals(inquiryMap.get("flag"))){
            logPaging.setPagingList((List<Log>) inquiryMap.get("data"));
        }
        request.setAttribute("operators",operators);
        request.setAttribute("logPaging",logPaging);
        request.getRequestDispatcher("logjsp/log.jsp").forward(request,response);
    }
}
